package com.seanzx.dp.common;

import com.seanzx.dp.entity.Column;
import com.seanzx.dp.serializer.ISerializer;
import com.seanzx.dp.serializer.SerializerFactory;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class DBFileInputStream extends InputStream {
    private final FilterInputStream inputStream;

    public DBFileInputStream(FilterInputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public int read() throws IOException {
        return inputStream.read();
    }

    public List<String> readRow(List<Column> columns) throws IOException{
        if (inputStream.available() == 0) {
            return null;
        }
        List<String> row = new ArrayList<>(columns.size());
        for (Column column : columns) {
            ISerializer serializer = SerializerFactory.getSerializer(column.getType());
            row.add(serializer.deserialize(column, this.inputStream));
        }
        return row;
    }

    public List<String> readRow(List<Column> columns, List<String> filterColumns) throws IOException{
        if (inputStream.available() == 0) {
            return null;
        }
        List<String> row = new ArrayList<>(columns.size());
        for (Column column : columns) {
            ISerializer serializer = SerializerFactory.getSerializer(column.getType());
            String value = serializer.deserialize(column, this.inputStream);
            if (filterColumns.contains(column.getName())) {
                row.add(value);
            }
        }
        return row;
    }

    public static void main(String[] args) {
        byte[] bytes = new byte[100];
        bytes[4] = 1;
//        bytes[]
        String str = new String(bytes);
        System.out.println(str);
    }
}
